package com.thor.palmmall.rest.hdmall.user.impl;

import static com.thor.commons.util.Assert.assertArgumentNotNull;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.exception.ThorServiceException;
import com.thor.palmmall.hdmall.user.HDMUser;
import com.thor.palmmall.hdmall.user.HDMUserService;
import com.thor.palmmall.rest.hdmall.user.MAuthenInfo;
import com.thor.palmmall.rest.hdmall.user.MUser;
import com.thor.palmmall.rest.hdmall.user.MUserService;
import com.thor.palmmall.rest.hdmall.user.converter.MAuthenInfoConverter;
import com.thor.palmmall.rest.hdmall.user.converter.MUserConverter;

/**
 * @author:liyang
 * @time:2017年7月25日 下午4:15:37
 * @since:1.0
 */
public class MUserServiceImpl implements MUserService {

  @Autowired
  HDMUserService hdmUserService;

  @Override
  public MUser login(MAuthenInfo authen) throws ThorServiceException {
    assertArgumentNotNull(authen, "authen");
    assertArgumentNotNull(authen.getPrincipal(), "authen.principal");
    assertArgumentNotNull(authen.getPassword(), "authen.password");
    assertArgumentNotNull(authen.getType(), "authen.type");

    HDMUser hdmUser = hdmUserService.authen(MAuthenInfoConverter.getInstance().convert(authen));
    if (hdmUser == null) {
      throw new ThorServiceException("指定用户不存在");
    } else {
      return MUserConverter.getInstance().convert(hdmUser);
    }
  }

}
